/**
 * 
 */
package com.brewdroid.db.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.brewdroid.db.DatabaseHelper;
import com.brewdroid.enumerators.FlocLevel;
import com.brewdroid.enumerators.YeastFormat;
import com.brewdroid.model.Yeast;

/**
 * @author meeusen
 *
 */
public class YeastDAO {

	private static final String YEAST_SELECT_BY_ID = "select * from yeast where _id=?";
	private static final String YEAST_SELECT_ALL="select * from yeast order by name";
	private static final String TABLE="yeast";
	private static final String NULL_COL="name";
	private static final String YEAST_SELECT_ID="select y._id from yeast y where y.name=? and y.description=? and y.format=? ";
	private static final String YEAST_SELECT_BY_NAME="select * from yeast where name=?";
	
	public static Yeast getYeastByName(DatabaseHelper db, String name)
	{
		Yeast y = null;
		Cursor c = db.getReadableDatabase().rawQuery(YEAST_SELECT_BY_NAME, new String[]{name});
		if(c.moveToFirst())
			y= makeYeastFromCursor(c);
		
		return y;
	}
	
	
	public static Yeast getYeastById(DatabaseHelper db, int id)
	{
		Cursor c = db.getReadableDatabase().rawQuery(YEAST_SELECT_BY_ID, new String[]{Integer.toString(id)});
		c.moveToFirst();
		Yeast y=makeYeastFromCursor(c);
		
		return y;
	}
	
	
	public static void addYeast(DatabaseHelper db, Yeast yeast)
	{	if(getId(db, yeast)==0)
		{
			ContentValues cv=new ContentValues();
			cv.put("description", yeast.getDescription());
			cv.put("name",yeast.getName());
			cv.put("format", yeast.getFormat().toString().toLowerCase());
			cv.put("optimumTemp", yeast.getOptimumTemp());
			cv.put("lowAttenuation",yeast.getLowAttenuation());
			cv.put("highAttenuation",yeast.getHighAttenuation());
			cv.put("flocculation", yeast.getFlocculation().toString().toLowerCase());
			
			db.getWritableDatabase().insert(TABLE, NULL_COL , cv);
		}

	}
	
	public static int getId(DatabaseHelper db, Yeast yeast)
	{
		int x= 0;
		Cursor c = db.getReadableDatabase().rawQuery(YEAST_SELECT_ID, new String[]{
																	yeast.getName(),
																	yeast.getDescription(),
																	yeast.getFormat().toString().toLowerCase()
		});
		
		x=c.moveToFirst()?c.getInt(0):0;
		
		return x;
	}
	
	
	public static ArrayList<Yeast> getYeasts(DatabaseHelper db)
	{
		ArrayList<Yeast> yeast= new ArrayList<Yeast>();
		Cursor c = db.getReadableDatabase().rawQuery(YEAST_SELECT_ALL, new String[]{});
		
		if(c.moveToFirst())
		{
			do{
				Yeast y = makeYeastFromCursor(c);
				yeast.add(y);
				
			}while(c.moveToNext());
		}
		return yeast;
	}

	public static ArrayList<String> getYeastStrings(DatabaseHelper dbHelper) {
		ArrayList<String> yeastStrings = new ArrayList<String>();
		for(Yeast i: getYeasts(dbHelper)){
			yeastStrings.add(i.getName());
		}
		
		return yeastStrings;
	}

	private static Yeast makeYeastFromCursor(Cursor c) {
		Yeast y = new Yeast();
		y.setDescription(c.getString(1));
		y.setName(c.getString(2));		
		String yForm=c.getString(3);
		y.setOptimumTemp(c.getFloat(4));
		y.setLowAttenuation(c.getInt(5));
		y.setHighAttenuation(c.getInt(6));		
		String yFloc=c.getString(7);
		//set the format
		if(yForm.equalsIgnoreCase("DRY"))
			y.setFormat(YeastFormat.DRY);
		else if(yForm.equalsIgnoreCase("VIAL"))
			y.setFormat(YeastFormat.VIAL);
		else if(yForm.equalsIgnoreCase("SMACKPACK"))
			y.setFormat(YeastFormat.SMACKPACK);
		//set the flocculation level
		
		if(yFloc.equalsIgnoreCase("VERY_LOW"))
			y.setFlocculation(FlocLevel.VERY_LOW);
		else if(yFloc.equalsIgnoreCase("LOW"))
			y.setFlocculation(FlocLevel.LOW);		
		else if(yFloc.equalsIgnoreCase("MEDIUM"))
			y.setFlocculation(FlocLevel.MEDIUM);	
		else if(yFloc.equalsIgnoreCase("HIGH"))
			y.setFlocculation(FlocLevel.HIGH);	
		else if(yFloc.equalsIgnoreCase("VERY_HIGH"))
			y.setFlocculation(FlocLevel.VERY_HIGH);	
		
		return y;
	}
	
}
